package it.blueocean.acanto.taglibrary.jsp.taglib.tags;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanPropertyNotFoundException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.MalformedPropertyExpressionException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HttpUtils;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BindController extends HttpServlet implements SubmitEventListener
{
	public static final String SUBMITTING_COMPONENT = "submittingComponent";

	private static final long serialVersionUID = 9891134532752L;

	private final static Logger logger_ = LoggerFactory.getLogger(BindController.class);

	public static final String REFERRER = "referrer";

	public static final String GOTO = "goto";

	public static final String AJAX = "ajax";

	public static final String AJAX_PARAM = "AJAX_PARAM";

	public static final String SERVLET_NAME = ".bind";

	protected void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
			throws ServletException, IOException
	{
		doPost(paramHttpServletRequest, paramHttpServletResponse);
	}

	protected void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
			throws ServletException, IOException
	{
		StateBeansManager localStateBeansManager = StateBeansManager.getInstance(paramHttpServletRequest.getSession());
		String str = paramHttpServletRequest.getServletPath();
		str = str.substring(str.lastIndexOf(47) + 1, str.length() - ".bind".length());
		
		Object localObject = this;
		
		if(str.length() != 0)
		{
			try
			{
				AbstractComponentStateBean localAbstractComponentStateBean = localStateBeansManager.getStateBean(str);
				if(localAbstractComponentStateBean instanceof SubmitEventListener)
					localObject = (SubmitEventListener) localAbstractComponentStateBean;
			}
			catch (ComponentStateBeanNotFoundException localComponentStateBeanNotFoundException)
			{
				logger_.error(localComponentStateBeanNotFoundException.getMessage());
			}
		}
			
		paramHttpServletRequest.setAttribute("submittingComponent", localObject);
		
		((SubmitEventListener) localObject).onSubmit(paramHttpServletRequest, paramHttpServletResponse);
	}

	public void onSubmit(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
			throws IOException
	{
		String str1 = paramHttpServletRequest.getParameter("goto");
		String str2 = paramHttpServletRequest.getParameter("ajax");
		
		if((str2 != null) && (str2.equalsIgnoreCase("true")))
		{
			StringBuffer localObject1 = new StringBuffer(str1);
			((StringBuffer) localObject1).append("?").append("AJAX_PARAM").append("=")
					.append(System.currentTimeMillis());
			str1 = ((StringBuffer) localObject1).toString();
		}
		
		Object localObject1 = StateBeansManager.getInstance(paramHttpServletRequest.getSession());
		Map localMap = paramHttpServletRequest.getParameterMap();
		Iterator localIterator = localMap.entrySet().iterator();
		
		boolean bool = false;
		Object localObject2;
		
		while (localIterator.hasNext())
		{
			localObject2 = (Map.Entry) localIterator.next();
			String str3 = (String) ((Map.Entry) localObject2).getKey();
			
			if((!(str3.equals("referrer"))) && (!(str3.equals("goto"))) && (!(str3.equals("ajax"))))
			{
				try
				{
					if(!(setBeanProperty(paramHttpServletRequest, (StateBeansManager) localObject1, bool, str3)))
						setBeanProperty(paramHttpServletRequest, (StateBeansManager) localObject1, bool, str3
								+ ".value");
				}
				catch (InvocationTargetException localInvocationTargetException)
				{
					logger_.debug(localInvocationTargetException.getCause().getMessage());
					bool = true;
				}
			}
		}
		if(bool)
		{
			localObject2 = paramHttpServletRequest.getParameter("referrer");
			paramHttpServletResponse.sendRedirect(HttpUtils.addContextPath((String) localObject2, paramHttpServletRequest));
		}
		else
		{
			paramHttpServletResponse.sendRedirect(HttpUtils.addContextPath(str1, paramHttpServletRequest));
		}
	}

	private boolean setBeanProperty(HttpServletRequest paramHttpServletRequest,
			StateBeansManager paramStateBeansManager, boolean paramBoolean, String paramString)
			throws InvocationTargetException
	{
		try
		{
			StateBeanPropertyLocator localStateBeanPropertyLocator = new StateBeanPropertyLocator(paramString);
			
			logger_.debug("========= setBeanProperty : " + paramString + " - values : "
					+ Arrays.toString(paramHttpServletRequest.getParameterValues(paramString)) + " - property name : "
					+ localStateBeanPropertyLocator.getPropertyName());
			
			AbstractComponentStateBean localAbstractComponentStateBean = paramStateBeansManager.getStateBean(localStateBeanPropertyLocator);
			
			localAbstractComponentStateBean.setProperty(localStateBeanPropertyLocator.getPropertyName(),
					paramHttpServletRequest.getParameterValues(paramString));
			
			return true;
		}
		catch (ComponentStateBeanNotFoundException localComponentStateBeanNotFoundException)
		{
			logger_.debug(localComponentStateBeanNotFoundException.getMessage());
			return false;
		}
		catch (ComponentStateBeanPropertyNotFoundException localComponentStateBeanPropertyNotFoundException)
		{
			logger_.debug(localComponentStateBeanPropertyNotFoundException.getMessage());
			return false;
		}
		catch (MalformedPropertyExpressionException localMalformedPropertyExpressionException)
		{
			logger_.debug(localMalformedPropertyExpressionException.getMessage());
		}
		
		return false;
	}
}
